package name.vaccari.matteo.tai.phonedirectory.persistence;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import name.vaccari.matteo.tai.phonedirectory.model.PeopleRepository;
import name.vaccari.matteo.tai.phonedirectory.model.Person;

public class InMemoryPeopleRepository implements PeopleRepository {

	private static final int N = 4;
	static List<Person> allPeople = new ArrayList<Person>();
	private static Names names;
	
	static {
		names = new Names();
		for (int i=0; i<N; i++) {
			Person person = generateRandomPerson();
			allPeople.add(person);
		}
		Collections.sort(allPeople);
	}
	
	private static Person generateRandomPerson() {
		Person result = new Person();
		result.setFirstName(names.getRandomName());
		result.setLastName(names.getRandomName());
		result.setMobile(names.getRandomPhoneNumber());
		result.setSkype(names.getRandomName().toLowerCase());
		result.setEmail(names.getRandomEmail(result.getFirstName(), result.getLastName()));
		return result;
	}

	public List<Person> getPeople(int startIndex, int howMany) {		
		int toIndex = Math.min(allPeople.size(), startIndex + howMany);
		return allPeople.subList(startIndex, toIndex);
	}
	
	public int getTotalPeople() {
		return allPeople.size();
	}

	public void add(Person person) {
		allPeople.add(person);
	}
}
